package com.lishao.soccer.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.lishao.soccer.bean.entity.MdDataItem;
import com.lishao.soccer.bean.entity.TmGameData;
import com.lishao.soccer.bean.entity.TmMember;
import com.lishao.soccer.bean.entity.TmTeamGame;
import com.lishao.soccer.service.MdDataItemService;
import com.lishao.soccer.service.TmGameDataService;
import com.lishao.soccer.service.TmTeamGameService;
import com.lishao.system.security.providers.SecurityUser;
import com.lishao.system.utils.ActionUtil;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

public class TmGameDataAction extends ActionSupport {
	TmGameDataService tmGameDataService;
	MdDataItemService mdDataItemService;
	TmTeamGameService tmTeamGameService;
	Long rowId;
	Long teamId;
	Long tmGameId;
	Long tmScoreListId;
	Date startDate;
	Date endDate;
	Long playUserId;
	Long itemId=null;
	String itemType=null;
	String jsonStr;

	public String selectGameDataByGame()throws Exception{
		List<TmGameData> gameDataList = tmGameDataService.selectDataByGame(teamId, tmGameId);
		Map resMap = new HashMap();
		resMap.put("gameDataList", gameDataList);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	
	public String selectAllDataItem() throws Exception{
		List<MdDataItem> dataItemList = mdDataItemService.selectAll();
		Map resMap = new HashMap();
		resMap.put("dataItemList", dataItemList);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	public String gameDataSave() throws Exception{
		String strMessage = "";
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		
		TmTeamGame tmTeamGame = tmTeamGameService.selectTeamGameById(tmGameId);
		if(true){//"30".equals(tmTeamGame.getStatusFlag())
			List <TmGameData> gameDataList = JSONArray.toList(JSONArray.fromObject(jsonStr),new TmGameData(),new JsonConfig());
			tmGameDataService.saveGameData(teamId, tmGameId, secUser, gameDataList);
			strMessage="操作成功！";
		}else{
			strMessage="当前比赛状态不允许维护比赛数据！";
		}
		
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	public String deleteGameDataById() throws Exception{
		String strMessage = "";
		int iResult = tmGameDataService.deleteById(rowId);
		if(iResult>0){
			strMessage="操作成功！";
		}else{
			strMessage="处理失败！";
		}
		Map resMap = new HashMap();
		resMap.put("strMessage", strMessage);
		new ActionUtil().writeMap(resMap);
		return null;
	}
	
	public String selectGameDataList() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();

		HttpSession session = request.getSession();
		SecurityUser secUser = (SecurityUser)session.getAttribute("LOGIN_KEY");
		
		List<TmGameData> gameDataList = null;
		if(tmScoreListId!=null&&tmScoreListId>0){
			gameDataList = tmGameDataService.selectGameDataList(tmScoreListId,playUserId,itemId,itemType);
		}else{
			gameDataList = tmGameDataService.selectGameDataList(teamId, tmGameId, startDate, endDate,playUserId,itemId,itemType);
		}
		Map resMap = new HashMap();
		resMap.put("gameDataList", gameDataList);
		new ActionUtil().writeMap(resMap);
		
		return null;
	}
	//get set
	public TmGameDataService getTmGameDataService() {
		return tmGameDataService;
	}

	public void setTmGameDataService(TmGameDataService tmGameDataService) {
		this.tmGameDataService = tmGameDataService;
	}

	public MdDataItemService getMdDataItemService() {
		return mdDataItemService;
	}

	public void setMdDataItemService(MdDataItemService mdDataItemService) {
		this.mdDataItemService = mdDataItemService;
	}

	public TmTeamGameService getTmTeamGameService() {
		return tmTeamGameService;
	}

	public void setTmTeamGameService(TmTeamGameService tmTeamGameService) {
		this.tmTeamGameService = tmTeamGameService;
	}

	public Long getRowId() {
		return rowId;
	}

	public void setRowId(Long rowId) {
		this.rowId = rowId;
	}

	public Long getTeamId() {
		return teamId;
	}

	public void setTeamId(Long teamId) {
		this.teamId = teamId;
	}

	public Long getTmGameId() {
		return tmGameId;
	}

	public void setTmGameId(Long tmGameId) {
		this.tmGameId = tmGameId;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}

	public Long getTmScoreListId() {
		return tmScoreListId;
	}

	public void setTmScoreListId(Long tmScoreListId) {
		this.tmScoreListId = tmScoreListId;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Long getPlayUserId() {
		return playUserId;
	}

	public void setPlayUserId(Long playUserId) {
		this.playUserId = playUserId;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public String getItemType() {
		return itemType;
	}

	public void setItemType(String itemType) {
		this.itemType = itemType;
	}
	
}
